package method.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileStreamDemo {
    public static void main(String[] args) {
        File file = new File("D:\\project\\demo1\\TestDemo\\src\\method\\io\\word.txt");
        try{
            FileOutputStream out = new FileOutputStream(file);
            byte[] buy = "我有一只小毛驴，我从来也不骑。有一天".getBytes();
            out.write(buy);
            out.close();
        }catch (IOException e){
            e.printStackTrace();
        }
        try{
            FileInputStream in = new FileInputStream(file);
            byte[] byt = new byte[1024];
            int len = in.read(byt);
            System.out.println("文件中的信息是："+new String(byt,0,len));       //读取byt,从0开始到len长度的bufen
            in.close();
        }catch (IOException e){
            e.printStackTrace();
        }
        File file1 = new File("Hello.txt");
        if (file1.exists()){
            file1.delete();                         //delete():删除文件
            System.out.println("文件已删除");
        }else {
            try{
                file1.createNewFile();
                System.out.println("文件已创建");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
